<template>
  <el-table
    :key="tableKey"
    v-loading="listLoading"
    :data="users"
    border
    highlight-current-row
    style="width: 100%;"
  >

    <!-- <el-table-column type="expand">
      <template slot-scope="{row, $index}">
        <el-form label-position="left" class="table-expand">
          <el-form-item label="用户头像">
            <el-image
              class="image"
              :src="row.image"
              :preview-src-list="[row.image]"
              fit="cover"
            >
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline" />
              </div>
            </el-image>
          </el-form-item>
          <el-form-item label="用户账号">
            <span>{{ row.account | hasValue() }}</span>
          </el-form-item>
          <el-form-item label="用户名称">
            <span>{{ row.userName | hasValue() }}</span>
          </el-form-item>
          <el-form-item label="权限角色">
            <span>{{ roles[userRole[$index].role] }}</span>
          </el-form-item>
          <el-form-item label="用户年龄">
            <span>{{ row.age | hasValue() }}</span>
          </el-form-item>
          <el-form-item label="用户性别">
            <span>{{ row.sex | hasValue() }}</span>
          </el-form-item>
          <el-form-item label="用户邮箱">
            <span>{{ row.email | hasValue() }}</span>
          </el-form-item>
          <el-form-item label="电话号码">
            <span>{{ row.phone | hasValue() }}</span>
          </el-form-item>
          <el-form-item label="用户地址">
            <span v-if="row.province === ''">{{ row.province | hasValue() }}</span>
            <span v-else>{{ row.province }} {{ row.city }} {{ row.county }}</span>
          </el-form-item>
          <el-form-item label="详细地址">
            <span>{{ row.location | hasValue() }}</span>
          </el-form-item>
        </el-form>
      </template>
    </el-table-column> -->

    <el-table-column
      label="编号"
      align="center"
      width="90px"
      type="index"
      :index="indexMethod"
    />
    <el-table-column label="用户名" align="center">
      <template slot-scope="{row}">
        <span>{{ row.userName | hasValue() }}</span>
      </template>
    </el-table-column>
    <el-table-column label="账号" align="center">
      <template slot-scope="{row}">
        <span>{{ row.account | hasValue() }}</span>
      </template>
    </el-table-column>
    <el-table-column label="邮箱" align="center">
      <template slot-scope="{row}">
        <span>{{ row.email | hasValue() }}</span>
      </template>
    </el-table-column>
    <!-- <el-table-column label="省份" align="center">
      <template slot-scope="{row}">
        <span>{{ row.province | hasValue() }}</span>
      </template>
    </el-table-column>
    <el-table-column label="城市" align="center">
      <template slot-scope="{row}">
        <span>{{ row.city | hasValue() }}</span>
      </template>
    </el-table-column>
    <el-table-column label="区、县" align="center">
      <template slot-scope="{row}">
        <span>{{ row.county | hasValue() }}</span>
      </template>
    </el-table-column> -->
    <el-table-column label="权限" align="center">
      <template slot-scope="{row, $index}">
        <span>{{ roles[userRole[$index].role] }}</span>
      </template>
    </el-table-column>

    <el-table-column
      label="操作"
      align="center"
      min-width="80px"
      class-name="small-padding fixed-width"
    ><template slot-scope="{row}">
      <el-button type="primary" size="mini" @click="goToUserDetailPage(row)">详情</el-button>
      <el-button
        type="danger"
        size="mini"
        @click="deleteAccount(row.account)"
      >
        删除
      </el-button>
    </template>
    </el-table-column>

  </el-table>
</template>

<script>
const rolesArr = [
  { value: 0, label: '超级管理员' },
  { value: 1, label: '基地管理员' },
  { value: 2, label: '普通用户' },
  { value: 4, label: '企业管理员' }
]
const rolesKeyValue = rolesArr.reduce((acc, cur) => {
  acc[cur.value] = cur.label
  return acc
}, {})

export default {
  name: 'MyTable',
  filters: {
    hasValue(value) {
      if (value === '') {
        return '暂未录入'
      } else {
        return value
      }
    }
  },
  props: {
    users: {
      type: Array,
      required: true
    },
    userRole: {
      type: Array,
      required: true
    },
    listLoading: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      tableKey: 0,
      roles: rolesKeyValue
    }
  },
  methods: {
    indexMethod(index) {
      return index + 1
    },
    deleteAccount(account) {
      this.$confirm('确定删除所选帐号吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$emit('del-account', account)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    goToUserDetailPage(row) {
      this.$router.push({
        path: '/userDetail',
        query: {
          account: row.account
        }
      })
    }
  }
}
</script>

<style scoped>
.image {
  width:100px;
  height:100px;
  border-radius:10px;
  box-shadow: 0 5px 5px 0 #999;
}

</style>
